package sg.edu.nus.iss.jass.gui;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import sg.edu.nus.iss.jass.constant.PrivilegeConstants;
import sg.edu.nus.iss.jass.controller.MainController;
import sg.edu.nus.iss.jass.privilege.Role;
import sg.edu.nus.iss.jass.privilege.SessionContext;

/**
 * <h5>MenuPanel to load the JTree Menu</h5>
 * This class is derived from JPanel to add the JTree for the user to
 * select the function.This Class impelemented the callback methods from TreeSelectionListener
 * to track the menu selected and launch the function panel based on the menu selection.
 * @author Kafoor Sammil
 * 
 */

public class MenuPanel extends JPanel implements TreeSelectionListener {

	private static final long serialVersionUID = 5647509434364112452L;
	
	/**
     * The menu tree to load the functions menu tree.
     */ 
	private JTree menuTree;
	
	/**
     * The width of the menu panel.
     */ 
	public static final int WIDTH = 1000;
	
	/**
     * The height of the menu panel.
     */ 
	public static final int HEIGHT = 650;
	
	/**
     * The reference to the container frame which loaded this panel.
     */ 
	private MainFrame mainframe = null;
	
	private MainController mainController ;
	
	/**
	 * Constructor to create the MenuPanel Object.
	 *  
	 */
	private MenuPanel(){
		super();
		this.menuTree = new JTree(createTreeNode());
		menuTree.addTreeSelectionListener(this);
		JScrollPane js = new JScrollPane(this.menuTree);
		js.setPreferredSize(new Dimension((int) (WIDTH * 0.20), HEIGHT));
		
		add(js);
		
	}
	
	/**
	 * Constructor to create the MenuPanel Object .
	 * @param mainFrame
	 *            reference to the container mainframe object. 
	 *  
	 */
	public MenuPanel(MainFrame mainFrame){
		this();
		this.mainframe = mainFrame;
		this.mainController = MainController.getInstance();
		this.mainController.init(mainFrame);
	}
	
	/**
	 * To build the TreeNode Object for creating the JTree Object .
	 *  
	 */
	private TreeNode createTreeNode() {
		
		Role role = SessionContext.getUser().getRole();
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("JASS");
		DefaultMutableTreeNode userTree = new DefaultMutableTreeNode(role.getRoleName());
		root.add(userTree);
		List<MenuFunction> accessibleFunctions = new ArrayList<MenuFunction>();
		
		
		accessibleFunctions = SessionContext.getUser().getRole().getFunctionList();
		for(int i=0; i < accessibleFunctions.size(); i++){
			userTree.add(new DefaultMutableTreeNode(accessibleFunctions.get(i)));
		}
	
			return root;

	}
	
	
	
	/**
	 * This is callback method from TreeSelectionListener , will be called whenever the value 
	 * of the selection changes.
	 * @param treeSelectionEvent
	 *            This is the event caused by the menu selection change. 
	 *  
	 */
	@Override
	public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
		// headerPanel.clearMsg();
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) this.menuTree.getLastSelectedPathComponent();
		if (node == null)
			return;
		String menuName = node.toString();
		Object menuObject = node.getUserObject();
		MenuFunction selectedFunction = null;

		// HeaderPanel headerPanel = new HeaderPanel("Add Job Advert");
		if (menuObject instanceof MenuFunction) {
			selectedFunction = (MenuFunction) menuObject;
			if (selectedFunction.getFunctionKey().equals(PrivilegeConstants.REGISTER_JOB_SEEKER)) {
				// this.rightPanel.remove(this.functionPanel);
				selectRegisterJobSeeker();
				// selectUseCase();

			} else if (selectedFunction.getFunctionKey().equals(PrivilegeConstants.LOGOUT_FUNC)) {
				doLogout();
			} else if (selectedFunction.getFunctionKey().equals(PrivilegeConstants.SEACH_JOB_AD)) {
				// this.rightPanel.remove(this.functionPanel);
				selectSearchJob();
				// selectUseCase();
			} else if (selectedFunction.getFunctionKey().equals(PrivilegeConstants.ADD_JOB_AD)) {
				selectAddJobAdvert();
			} else if (selectedFunction.getFunctionKey().equals(PrivilegeConstants.LISTING_JOB_AD)) {
				selectSearchJob();
			}
			
			/* End Added */

			mainframe.setStatusBar(menuName);
			mainframe.getFunctionPanel().revalidate();
			return;
		}
	}
	
	private void selectSearchJob() {
		mainController.searchJobAd();
		
	}

	private void selectRegisterJobSeeker() {
		mainController.registerJobSeeker();
	}
	
	private void selectAddJobAdvert() {
		mainController.startAddJobAd();
	}
	
	private void startTestingViewJobAd() {
		mainController.startTestingViewJobAd();
	}
	
	
	/**
	 * Private method which will call the  actuallogout function from the mainframe.
	 */
	private void doLogout(){
		int opt= JOptionPane.showConfirmDialog(null, "Log Out");
		if (opt == JOptionPane.YES_OPTION){
			 mainframe.doLogout();
		}    else{
			menuTree.setSelectionPath(menuTree.getLeadSelectionPath());
		}
	}
	

	/**
	 * To get the  menuTree of the MenuPanel  
	 * @return the menuTree
	 */
	public JTree getMenuTree() {
		return menuTree;
	}

	/**
	 * To set the  menuTree of the MenuPanel   
	 * @param menuTree the menuTree to set
	 */
	public void setMenuTree(JTree menuTree) {
		this.menuTree = menuTree;
	}

}
